package com.doumuxie.controller;

import com.doumuxie.common.dto.UserDto;
import com.doumuxie.service.UserService;
import com.doumuxie.util.IPUtil;
import com.doumuxie.util.ResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/11/30 19:20
 * @description user controller
 **/
@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
public class UserController {


    final UserService userService;

    /**
     * 注册接口
     *
     * @param dto 参数
     * @return 统一返回
     */
    @GetMapping("/reg")
    public ResultUtil reg(UserDto dto) {
        return userService.regUser(dto);
    }


    /**
     * 根据token获取用户信息
     *
     * @param token token
     * @return 用户信息
     */
    @GetMapping("/info")
    public ResultUtil info(String token) {
        return userService.userInfo(token);
    }


    /**
     * 登录接口
     *
     * @param dto 参数
     * @return 统一返回
     */
    @GetMapping("/login")
    public ResultUtil login(UserDto dto, HttpServletRequest request) {
        return userService.login(dto, IPUtil.getIpAddress(request));
    }


//    @GetMapping("/get")
//    public ResultUtil get(User user) {
//        User one = userRepository.getOne(user.getId());
//        UserDto result = BeanUtil.copy(one, UserDto.class);
//        return ResultUtil.success(result);
//    }
}
